package mybatis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import mybatis.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.ApiResult;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;

/**
 * @author: Atosw
 * @date: 2024/11/10
 * @version: 1.0
 * @description:
 */
public class BaseController<K extends BaseService<T>, T, P extends Serializable> {

    @Autowired
    private K baseService;

    @PostMapping
    public ApiResult<Boolean> save(@RequestBody T t){
        return new ApiResult<>(baseService.save(t));
    }

    @PutMapping
    public ApiResult<Boolean> update(@RequestBody T t){
        return new ApiResult<>(baseService.updateById(t));
    }

    @DeleteMapping("/{id}")
    public ApiResult<Boolean> delete(@PathVariable("id") P id){
        return new ApiResult<>(baseService.removeById(id));
    }

    @GetMapping("/{id}")
    public ApiResult<T> getById(@PathVariable("id") String id){
        return new ApiResult<>(baseService.getById(id));
    }

    @GetMapping("/page")
    public ApiResult<IPage<T>> pageByQuery(IPage<T> t){
        return new ApiResult<>(baseService.pageByQuery(t));
    }

    @GetMapping
    public ApiResult<Collection<T>> selectAll(){
        return new ApiResult<>(baseService.listByMap(new HashMap<>()));
    }

}
